/*
 * $Id: utl_canceltimedhalt.c,v 1.2 2006-03-21 16:15:16 bacon Exp $
 */

#include <xp/sys/utl.h>

int xp_canceltimedhalt (void)
{
#ifdef _WIN32
	HANDLE token;
	TOKEN_PRIVILEGES priv;

	if (OpenProcessToken(GetCurrentProcess(),
		TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token) == FALSE) return -1;

	LookupPrivilegeValue (XP_NULL, SE_SHUTDOWN_NAME, &priv.Privileges[0].Luid);

	priv.PrivilegeCount = 1;
	priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
	if (AdjustTokenPrivileges (token, FALSE, 
		&priv, 0, (TOKEN_PRIVILEGES*)XP_NULL, 0) == FALSE) 
	{
		CloseHandle (token);
		return -1;

	}
	if (GetLastError() != ERROR_SUCCESS) 
	{
		/* disable shutdown privilege */
		priv.Privileges[0].Attributes = 0; 
		AdjustTokenPrivileges (token, FALSE, 
			&priv, 0, (TOKEN_PRIVILEGES*)XP_NULL, 0); 
		CloseHandle (token);
		return -1;
	}

	if (AbortSystemShutdown(XP_NULL) == FALSE) 
	{
		/* disable shutdown privilege */
		priv.Privileges[0].Attributes = 0; 
		AdjustTokenPrivileges (token, FALSE, 
			&priv, 0, (TOKEN_PRIVILEGES*)XP_NULL, 0); 
		CloseHandle (token);
		return -1;
	}

	/* disable shutdown privilege */
	priv.Privileges[0].Attributes = 0; 
	AdjustTokenPrivileges (token, FALSE, 
		&priv, 0, (TOKEN_PRIVILEGES*)XP_NULL, 0); 
	CloseHandle (token);
	return 0;
#else
	return -1;
#endif
}
